package com.danjinwulian.iot.service.impl;

import com.danjinwulian.iot.TencentIotClient;
import com.danjinwulian.iot.service.TencentIotService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 腾讯云物联网开发平台API服务实现
 * 专注于使用API接口进行产品和设备管理
 * 
 * @author ken.h
 */
@Service
public class TencentIotApiServiceImpl implements TencentIotService {

    private static final Logger logger = LoggerFactory.getLogger(TencentIotApiServiceImpl.class);

    @Autowired
    private TencentIotClient tencentIotClient;

    @Override
    public CreateStudioProductResponse createProduct(String productName, Long productType, String encryptionType) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            CreateStudioProductRequest req = new CreateStudioProductRequest();
            req.setProductName(productName);
            req.setProductType(productType);
            req.setEncryptionType(encryptionType);
            
            logger.info("调用腾讯云API创建产品: {}", productName);
            return client.CreateStudioProduct(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API创建产品失败: {}", e.getMessage(), e);
            throw new RuntimeException("创建产品失败: " + e.getMessage());
        }
    }

    @Override
    public ModifyStudioProductResponse modifyProduct(String productId, String productName) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            ModifyStudioProductRequest req = new ModifyStudioProductRequest();
            req.setProductId(productId);
            req.setProductName(productName);
            
            logger.info("调用腾讯云API修改产品: {} -> {}", productId, productName);
            return client.ModifyStudioProduct(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API修改产品失败: {}", e.getMessage(), e);
            throw new RuntimeException("修改产品失败: " + e.getMessage());
        }
    }

    @Override
    public DeleteStudioProductResponse deleteProduct(String productId) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            DeleteStudioProductRequest req = new DeleteStudioProductRequest();
            req.setProductId(productId);
            
            logger.info("调用腾讯云API删除产品: {}", productId);
            return client.DeleteStudioProduct(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API删除产品失败: {}", e.getMessage(), e);
            throw new RuntimeException("删除产品失败: " + e.getMessage());
        }
    }

    @Override
    public DescribeStudioProductResponse getProductDetail(String productId) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            DescribeStudioProductRequest req = new DescribeStudioProductRequest();
            req.setProductId(productId);
            
            logger.info("调用腾讯云API获取产品详情: {}", productId);
            return client.DescribeStudioProduct(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API获取产品详情失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取产品详情失败: " + e.getMessage());
        }
    }

    @Override
    public GetStudioProductListResponse getProductList(Long offset, Long limit) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            GetStudioProductListRequest req = new GetStudioProductListRequest();
            req.setOffset(offset);
            req.setLimit(limit);
            
            logger.info("调用腾讯云API获取产品列表: offset={}, limit={}", offset, limit);
            return client.GetStudioProductList(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API获取产品列表失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取产品列表失败: " + e.getMessage());
        }
    }

    @Override
    public CreateDeviceResponse createDevice(String productId, String deviceName) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            CreateDeviceRequest req = new CreateDeviceRequest();
            req.setProductId(productId);
            req.setDeviceName(deviceName);
            
            logger.info("调用腾讯云API创建设备: {} -> {}", productId, deviceName);
            return client.CreateDevice(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API创建设备失败: {}", e.getMessage(), e);
            throw new RuntimeException("创建设备失败: " + e.getMessage());
        }
    }

    @Override
    public DeleteDeviceResponse deleteDevice(String productId, String deviceName) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            DeleteDeviceRequest req = new DeleteDeviceRequest();
            req.setProductId(productId);
            req.setDeviceName(deviceName);
            
            logger.info("调用腾讯云API删除设备: {} -> {}", productId, deviceName);
            return client.DeleteDevice(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API删除设备失败: {}", e.getMessage(), e);
            throw new RuntimeException("删除设备失败: " + e.getMessage());
        }
    }

    @Override
    public DescribeDeviceResponse getDeviceDetail(String productId, String deviceName) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            DescribeDeviceRequest req = new DescribeDeviceRequest();
            req.setProductId(productId);
            req.setDeviceName(deviceName);
            
            logger.info("调用腾讯云API获取设备详情: {} -> {}", productId, deviceName);
            return client.DescribeDevice(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API获取设备详情失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取设备详情失败: " + e.getMessage());
        }
    }

    @Override
    public GetDeviceListResponse getDeviceList(String productId, Long offset, Long limit) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            GetDeviceListRequest req = new GetDeviceListRequest();
            req.setProductId(productId);
            req.setOffset(offset);
            req.setLimit(limit);
            
            logger.info("调用腾讯云API获取设备列表: {} -> offset={}, limit={}", productId, offset, limit);
            return client.GetDeviceList(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API获取设备列表失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取设备列表失败: " + e.getMessage());
        }
    }

    @Override
    public ControlDeviceDataResponse controlDevice(String productId, String deviceName, String data) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            ControlDeviceDataRequest req = new ControlDeviceDataRequest();
            req.setProductId(productId);
            req.setDeviceName(deviceName);
            req.setData(data);
            
            logger.info("调用腾讯云API控制设备: {} -> {} -> {}", productId, deviceName, data);
            return client.ControlDeviceData(req);
        } catch (TencentCloudSDKException e) {
            logger.error("调用腾讯云API控制设备失败: {}", e.getMessage(), e);
            throw new RuntimeException("控制设备失败: " + e.getMessage());
        }
    }

    @Override
    public PublishMessageResponse publishMessage(String productId, String deviceName, String topic, String payload) {
        try {
            IotexplorerClient client = tencentIotClient.getClient();
            
            PublishMessageRequest req = new PublishMessageRequest();
            req.setProductId(productId);
            req.setDeviceName(deviceName);
            req.setTopic(topic);
            req.setPayload(payload);
            
            logger.info("调用腾讯云API发布消息: {} -> {} -> {} -> {}", productId, deviceName, topic, payload);
            return client.PublishMessage(req);
        } catch (TencentCloudSDKException e) {
            String message = e.getMessage();
            if (message.contains("设备未订阅Topic")) {
                message += ": " + topic;
            }
            logger.error("调用腾讯云API发布消息失败: {}", message, e);
            throw new RuntimeException("发布消息失败: " + message);
        }
    }
}
